package cn.blog.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.blog.common.LayuiTableResult;
import cn.blog.common.PublicResultJosn;
import cn.blog.dao.MatchMapper;
import cn.blog.dao.MatchPKInfoMapper;
import cn.blog.dao.PlayerUserMapper;
import cn.blog.dto.MatchPKInfoSelect;
import cn.blog.entity.Match;
import cn.blog.entity.MatchPKInfo;
import cn.blog.entity.MatchPKInfoExample;
import cn.blog.entity.MatchPKInfoExample.Criteria;
import cn.blog.entity.PlayerUser;
import cn.blog.service.MatchPKInfoService;

@Service
public class MatchPKInfoServiceImpl implements MatchPKInfoService {

	@Autowired
	private MatchPKInfoMapper pkInfoMapper;

	@Autowired
	private PlayerUserMapper playerUserMapper;

	@Autowired
	private MatchMapper matchMapper;

	/**
	 * 添加一场比赛
	 * 
	 * @param matchPKInfo
	 * @return
	 */
	@Override
	@Transactional
	public PublicResultJosn add(MatchPKInfo matchPKInfo) {
		// 补全信息
		matchPKInfo.setPkid(UUID.randomUUID().toString());
		// 校验playerid是否存在
		if (this.checkPlayerUid(matchPKInfo.getFirstplayerid())) {
			throw new IllegalArgumentException("选手不存在");
		}
		if (this.checkPlayerUid(matchPKInfo.getSecondplayerid())) {
			throw new IllegalArgumentException("选手不存在");
		}
		if (matchPKInfo.getFirstplayerid().equals(matchPKInfo.getSecondplayerid())) {
			throw new IllegalArgumentException("pk选手不能是同一人");
		}
		matchPKInfo.setFirstneedscore((float) 0);
		matchPKInfo.setFirstscore((float) 0);
		matchPKInfo.setFirstticketcount(0);
		matchPKInfo.setSecondneedscore((float) 0);
		matchPKInfo.setSecondscore((float) 0);
		matchPKInfo.setSecondticketcount(0);
		// 对战状态 1:开启，0:关闭
		matchPKInfo.setStatus(0);
		pkInfoMapper.insert(matchPKInfo);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), matchPKInfo);
	}

	/**
	 * 单、批量删除比赛信息
	 * 
	 * @param ids
	 * @return
	 */
	@Override
	@Transactional
	public PublicResultJosn delete(List<String> ids) {
		MatchPKInfoExample example = new MatchPKInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andPkidIn(ids);
		pkInfoMapper.deleteByExample(example);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
	}

	/**
	 * 根据pkid更新比赛信息
	 * 
	 * @param matchPKInfo
	 * @return
	 */
	@Override
	@Transactional
	public PublicResultJosn update(MatchPKInfo matchPKInfo) {
		MatchPKInfo pkInfo = new MatchPKInfo();
		pkInfo.setPkid(matchPKInfo.getPkid());
		pkInfo.setStatus(matchPKInfo.getStatus());
		pkInfoMapper.updateByPrimaryKeySelective(pkInfo);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
	}

	/**
	 * 根据比赛信息模糊查询
	 * 
	 * @param matchPKInfo
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@Override
	public LayuiTableResult query(MatchPKInfo matchPKInfo, Integer page, Integer pageSize) {
		LayuiTableResult resultJosn = new LayuiTableResult();
		MatchPKInfoExample example = new MatchPKInfoExample();
		Criteria criteria = example.createCriteria();
		// 构造条件
		if (StringUtils.isNotBlank(matchPKInfo.getFirstsongname())) {
			criteria.andFirstsongnameLike(matchPKInfo.getFirstsongname());
		}
		Criteria criteria2 = example.createCriteria();
		if (StringUtils.isNotBlank(matchPKInfo.getSecondsongname())) {
			criteria.andSecondsongnameLike(matchPKInfo.getSecondsongname());
		}
		example.or(criteria2);
		// 开启分页查找
		PageHelper.startPage(page, pageSize);
		List<MatchPKInfo> list = pkInfoMapper.selectByExample(example);
		PageInfo<MatchPKInfo> pageInfo = new PageInfo<>(list, pageSize);
		//
		resultJosn.setCode(HttpStatus.OK.value());
		resultJosn.setMsg(HttpStatus.OK.getReasonPhrase());
		resultJosn.setCount(pageInfo.getTotal());
		resultJosn.setData(pageInfo.getList());
		return resultJosn;
	}

	/**
	 * 根据matchid查询比赛详情
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public PublicResultJosn queryOne(String id) {
		Map<String, Object> map = new HashMap<>();
		// 查询比赛基本信息
		Match match = matchMapper.selectByPrimaryKey(id);
		map.put("match", match);
		// 查询比赛详情
		List<MatchPKInfoSelect> result = new ArrayList<>();
		List<MatchPKInfoSelect> list = pkInfoMapper.selectByMatchId(id);
		if (!CollectionUtils.isEmpty(list)) {
			// 查找选手姓名
			for (MatchPKInfoSelect matchPKInfoSelect : list) {
				matchPKInfoSelect.setFirstPlayer(this.selectNameByPuid(matchPKInfoSelect.getFirstplayerid()));
				matchPKInfoSelect.setSecondPlayer(this.selectNameByPuid(matchPKInfoSelect.getSecondplayerid()));
				result.add(matchPKInfoSelect);
			}
		}
		map.put("data", result);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), map);
	}

	/**
	 * 检查puid是否存在，ture:不存在，false:存在，可以使用
	 * 
	 * @param puid
	 * @return
	 */
	private Boolean checkPlayerUid(String puid) {
		PlayerUser playerUser = playerUserMapper.selectByPrimaryKey(puid);
		if (null == playerUser) {
			return true;
		}
		return false;

	}

	/**
	 * 获取所有pk用户
	 * 
	 * @return
	 */
	@Override
	public PublicResultJosn queryPKuser() {
		List<PlayerUser> list = playerUserMapper.queryPKuser();
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), list);
	}

	/**
	 * 根据puid查询姓名
	 * 
	 * @return
	 */
	private PlayerUser selectNameByPuid(String puid) {
		PlayerUser user = playerUserMapper.selectByPrimaryKey(puid);
		return user;
	}

}
